package com.zycx.system.menu.service;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.zycx.common.entity.CamelMap;
import com.zycx.common.entity.EntityUtils;
import com.zycx.system.menu.entity.Menu;
import com.zycx.system.menu.mapper.MenuMapper;
import com.zycx.common.page.QueryPage;
import com.zycx.common.page.ServerResponseTable;
import com.zycx.common.result.ServerResponse;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.zycx.interceptor.CurrentUser;

import java.util.Date;
import java.util.List;
import java.util.Map;

/**
 * Description:
 * Created by zycx on 2018-12-10 21:40
 * Created with IntelliJ IDEA.
 */
@Service
@Slf4j
public class MenuService {

    @Autowired
    private MenuMapper menuMapper;

    public ServerResponseTable page(Map<String, Object> params) {
        QueryPage query = new QueryPage(params);
        PageHelper.startPage(query.getPage(), query.getLimit());
        List<CamelMap> list = menuMapper.findPage(query);
        PageInfo<CamelMap> info = new PageInfo<>(list);
        return new ServerResponseTable(info.getTotal(), list);
    }

    public ServerResponse saveMenu(Menu menu) {
        if (menu == null) {
            return ServerResponse.errorMessage("对象不能为空");
        }
        menu.setId(EntityUtils.getId());
        menu.setCtr_user_id(CurrentUser.getUserID());
        menu.setCtrTime(new Date());
        menu.setCtrUserName(CurrentUser.getName());
        int insert = menuMapper.insert(menu);
        if (insert == 0) {
            return ServerResponse.error();
        }
        return ServerResponse.successData(menu);
    }

    public ServerResponse getMenu(String id) {
        if (StringUtils.isEmpty(id)) {
            return ServerResponse.errorMessage("id 不能为空");
        }
        Menu menu = menuMapper.selectByPrimaryKey(id);
        if (menu == null) {
            return ServerResponse.errorMessage("数据不存在");
        }
        return ServerResponse.successData(menu);
    }

    public ServerResponse putMenu(Menu menu) {
        if (menu == null) {
            return ServerResponse.errorMessage("对象不能为空");
        }
        if (StringUtils.isEmpty(menu.getId())) {
            return ServerResponse.errorMessage("id 不能为空");
        }
        menu.setCtr_user_id(CurrentUser.getUserID());
        menu.setCtrUserName(CurrentUser.getName());
        menu.setCtrTime(new Date());
        try {
            menuMapper.putMenu(menu);
        } catch (Exception e) {
            log.info("修改菜单失败: " + e.getMessage());
            throw new RuntimeException("操作失败");
        }
        return ServerResponse.successData(menu);
    }

    public ServerResponse deleteMenu(String id) {
        if (StringUtils.isEmpty(id)) {
            return ServerResponse.errorMessage("id 不能为空");
        }
        Menu menu = menuMapper.selectByPrimaryKey(id);
        if (menu == null) {
            return ServerResponse.errorMessage("对象不存在,请刷新数据");
        }
        menu.setEnable(false);
        int i = menuMapper.updateByPrimaryKey(menu);
        if (i == 0) {
            return ServerResponse.error();
        }
        return ServerResponse.success();
    }


}
